import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-01-22
 * Time: 22:12
 */
public class Main {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            }else {
                if(stack.empty()) {
                    return false;
                }
                char ch1 = stack.peek();
                if(ch == ')' && ch1 == '(' ||ch == '}' && ch1 == '{'||ch == ']' && ch1 == '[') {
                    stack.pop();
                }else {
                    return false;
                }

            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Main main = new Main();
        System.out.println(main.isValid("(())"));
    }
}
